package main

import (
	"encoding/json"
	"flag"
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"strings"
	"time"
)

type JsonData struct {
	Mac      string `json:"mac"`
	Isp      string `json:"isp"`
	Net_err  string `json:"net_err"`
	Province string `json:"province"`
}

type GetData struct {
	Errno   int
	Error   string
	Payload struct {
		Data []struct {
			Avg_bw                     float64
			Env_err                    string
			Env_err_cnt                int
			Error_level                string
			Isp                        string
			Mac                        string
			Net_err                    string
			Net_err_cnt                int
			Offline_disable            string
			Offline_disable_suggestion string
			Province                   string
			Storage_err                string
			Storage_err_cnt            int
		}
		Dt       string
		Provider string
	}
}

func NewGetData() *GetData {
	return &GetData{Payload: struct {
		Data []struct {
			Avg_bw                     float64
			Env_err                    string
			Env_err_cnt                int
			Error_level                string
			Isp                        string
			Mac                        string
			Net_err                    string
			Net_err_cnt                int
			Offline_disable            string
			Offline_disable_suggestion string
			Province                   string
			Storage_err                string
			Storage_err_cnt            int
		}
		Dt       string
		Provider string
	}{Data: make([]struct {
		Avg_bw                     float64
		Env_err                    string
		Env_err_cnt                int
		Error_level                string
		Isp                        string
		Mac                        string
		Net_err                    string
		Net_err_cnt                int
		Offline_disable            string
		Offline_disable_suggestion string
		Province                   string
		Storage_err                string
		Storage_err_cnt            int
	}, 1)}}
}

func main() {
	mytime := flag.String("time", time.Now().Format("20060102"), "指定需要获取的时间！")
	//fmt.Println(*mytime)
	arg := flag.String("mac", "all", "查询所有mac address,或者对应mac的status！")
	flag.Parse()
	datastuct := NewGetData()
	cookie := PostLoadGetCookie()
	statudata := ApiOPS(cookie, *mytime)
	json.Unmarshal(statudata, datastuct)
	//fmt.Println(datastuct.Errno, datastuct.Error, datastuct.Payload.Dt, datastuct.Payload.Provider)
	switch *arg {
	case "all":
		for _, statdata := range datastuct.Payload.Data {
			fmt.Println(statdata.Mac)
		}
	default:
		for i, statdata := range datastuct.Payload.Data {
			if statdata.Mac == *arg {
				// fmt.Println(
				// 	//datastuct.Payload.Data[i].Avg_bw, "\n",
				// 	// datastuct.Payload.Data[i].Env_err+"\n",
				// 	// datastuct.Payload.Data[i].Env_err_cnt, "\n",
				// 	// datastuct.Payload.Data[i].Error_level+"\n",
				// 	"macaddr：", datastuct.Payload.Data[i].Mac, "\n",
				// 	"运营商：", datastuct.Payload.Data[i].Isp, "\n",
				// 	"网络：", datastuct.Payload.Data[i].Net_err, "\n",
				// 	// datastuct.Payload.Data[i].Net_err_cnt, "\n",
				// 	// datastuct.Payload.Data[i].Offline_disable+"\n",
				// 	// datastuct.Payload.Data[i].Offline_disable_suggestion+"\n",
				// 	"省份：", datastuct.Payload.Data[i].Province,
				// 	//datastuct.Payload.Data[i].Storage_err
				// )
				var Defjsondata = &JsonData{Mac: datastuct.Payload.Data[i].Mac, Isp: datastuct.Payload.Data[i].Isp, Net_err: datastuct.Payload.Data[i].Net_err, Province: datastuct.Payload.Data[i].Province}
				jsondata, err := json.Marshal(Defjsondata)
				if err != nil {
					fmt.Println(err)
					return
				}
				fmt.Println(string(jsondata))
				return
			}
		}
		fmt.Println("没有查询到mac为", *arg, "的相关信息！！")
	}
}

// 使用用户名和密码登录web并且返回cookie
func PostLoadGetCookie() string {
	client := &http.Client{}
	var data = strings.NewReader(`{"user_name":"用户","password":"密码","login_type":"normal"}`)
	req, err := http.NewRequest("POST", "登录api的url", data)
	if err != nil {
		log.Fatal(err)
	}
	req.Header.Set("Connection", "keep-alive")
	req.Header.Set("Accept", "application/json, text/plain, */*")
	req.Header.Set("Access-Control-Allow-Credentials", "true")
	req.Header.Set("Content-Type", "application/json")
	req.Header.Set("sec-ch-ua-mobile", "?0")
	req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.55 Safari/537.36 Edg/96.0.1054.34")
	req.Header.Set("Origin", "https://open.fog.qq.com")
	req.Header.Set("Sec-Fetch-Site", "same-origin")
	req.Header.Set("Sec-Fetch-Mode", "cors")
	req.Header.Set("Sec-Fetch-Dest", "empty")
	req.Header.Set("Referer", "https://open.fog.qq.com/")
	req.Header.Set("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6")
	resp, err := client.Do(req)
	if err != nil {
		log.Fatal(err)
	}
	// fmt.Println(strings.Split(strings.Split(resp.Header.Get("Set-Cookie"), ";")[0], "=")[1])
	return strings.Split(strings.Split(resp.Header.Get("Set-Cookie"), ";")[0], "=")[1]
}

// 使用cookie重新请求需要的api
func ApiOPS(cookie, timestamp string) []byte {
	client := &http.Client{}
	var data = strings.NewReader(`{"dt":"` + timestamp + `"}`)
	req, err := http.NewRequest("POST", "需要请求的url", data)
	if err != nil {
		log.Fatal(err)
	}
	req.Header.Set("Connection", "keep-alive")
	req.Header.Set("Accept", "application/json, text/plain, */*")
	req.Header.Set("Access-Control-Allow-Credentials", "true")
	req.Header.Set("Content-Type", "application/json")
	req.Header.Set("sec-ch-ua-mobile", "?0")
	req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36")
	req.Header.Set("Origin", "https://open.fog.qq.com")
	req.Header.Set("Sec-Fetch-Site", "same-origin")
	req.Header.Set("Sec-Fetch-Mode", "cors")
	req.Header.Set("Sec-Fetch-Dest", "empty")
	req.Header.Set("Referer", "https://open.fog.qq.com/")
	req.Header.Set("Accept-Language", "zh-CN,zh;q=0.9")
	req.Header.Set("Cookie", "fog-login-type=normal; sidebarStatus=0; session="+cookie)
	resp, err := client.Do(req)
	if err != nil {
		log.Fatal(err)
	}
	bodyText, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		log.Fatal(err)
	}
	//fmt.Printf("%s\n", bodyText)
	return bodyText
}
